<template>
  <div class="container">
    <div class="top">
      <!-- <el-button
        size="small"
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      > -->
      <el-button
        size="small"
        type="primary"
        class="handleSearch"
        icon="el-icon-edit"
        @click="addGoods()"
        >订单添加</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
    >
      <el-table-column
        prop="serial_code"
        label="订单号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="statusMsg"
        label="订单状态"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="org_name"
        label="客户名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="org_code"
        label="客户编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="add_time"
        label="下单时间"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" width="420" align="center">
        <template slot-scope="scope">
          <el-button
            type="info"
            size="mini"
            icon="el-icon-info"
            @click="handleEdit(scope)"
            >详情</el-button
          >
          <el-button
            type="danger"
            size="mini"
            v-if="scope.row.status == 0"
            icon="el-icon-delete"
            @click="eduitfication(scope)"
            >删除</el-button
          >
          <el-button
            type="primary"
            size="mini"
            v-if="scope.row.status == 0"
            icon="el-icon-edit"
            @click="modification(scope)"
            >编辑</el-button
          >
          <el-button
            type="success"
            size="mini"
            v-if="scope.row.status == 0"
            icon="el-icon-circle-check"
            @click="complete(scope)"
            >审核</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <middlepartcularsDialog
      :particularsDialogs="particularsDialogs"
    ></middlepartcularsDialog>
    <middleorderModification
      :detailsDialog="detailsDialog"
      @upData="getData"
    ></middleorderModification>
    <addmiddleGoodDialog
      :addDialog="addDialog"
      @upData="getData"
    ></addmiddleGoodDialog>
  </div>
</template>
<script>
import { getToken } from "@/utils/auth";
import Pagination from "@/components/pagination";
import {
  getSaleOrderList,
  checkSaleOrder,
  delSaleOrder,
} from "@/api/middleorder/middleorderlist";
import AddmiddleGoodDialog from "@/components/middleOrder/addmiddleGoodDialog";
import MiddleorderModification from "@/components/middleOrder/middleorderModification";
import MiddlepartcularsDialog from "@/components/middleOrder/middlepartcularsDialog";
export default {
  components: {
    pagination: Pagination,
    middlepartcularsDialog: MiddlepartcularsDialog,
    middleorderModification: MiddleorderModification,
    addmiddleGoodDialog: AddmiddleGoodDialog,
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },
      addDialog: {
        centerDialogVisible: false,
      },
      detailsDialog: {
        centerDialogVisible: false,
        id: "",
        erp_org_code: "",
      },
      particularsDialogs: {
        centerDialogVisible: false,
        id: "",
      },
      date: [],
    };
  },
  created() {
    //页面数据
    this.getData();
  },
  methods: {
    //页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
      };
      getSaleOrderList(data)
        .then((res) => {
          if (res.code == 0) {
            res.data.paginate.data.forEach((item) => {
              switch (item.status) {
                case 0:
                  item.statusMsg = "待审核";
                  break;
                case 1:
                  item.statusMsg = "已审核";
                  break;
              }
            });
            this.tableData = res.data.paginate.data;
            this.pageData.page = res.data.paginate.current_page;
            this.pageData.pagesize = res.data.paginate.per_page;
            this.pageData.total = res.data.paginate.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },

    //编辑
    modification(scope) {
      this.detailsDialog.centerDialogVisible = true;
      this.detailsDialog.id = scope.row.id;
      this.detailsDialog.erp_org_code = scope.row.erp_org_code;
    },
    //详情
    handleEdit(scope) {
      this.particularsDialogs.centerDialogVisible = true;
      this.particularsDialogs.id = scope.row.id;
    },
    //删除
    eduitfication(scope) {
      this.$confirm("是否确认删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            id: scope.row.id,
          };
          delSaleOrder(data)
            .then((res) => {
              if (res.code == 0) {
                this.$message({
                  type: "success",
                  message: res.msg,
                });
                this.getData();
              }
            })
            .catch((err) => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 确认审核
    complete(scope) {
      this.$confirm("是否确认审核?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            id: scope.row.id,
          };
          checkSaleOrder(data)
            .then((res) => {
              if (res.code == 0) {
                this.$message({
                  type: "success",
                  message: res.msg,
                });
                this.getData();
              }
            })
            .catch((err) => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消审核",
          });
        });
    },

    // 本地搜索
    handleSearch() {
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },

    //添加商品
    addGoods() {
      this.addDialog.centerDialogVisible = true;
    },
    //搜索清除
    empty() {
      this.date = [];
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  },
};
</script>
<style scoped>
.top {
  position: relative;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>